package com.ssfeng.proxy2;

import cn.hutool.core.io.FileUtil;

/**
 * Created by liyuhang on 2020-09-10.
 */
public class MyClassLoader extends ClassLoader {

    @Override
    protected Class<?> findClass(String name) {
        //class文件路径
        String classPath = MyClassLoader.class.getResource("").getPath() + "/" + name + ".class";
        System.out.println(classPath);
        //class的包名+文件名
        String className = MyClassLoader.class.getPackage().getName() + "." + name;
        System.out.println(className);

        //读取class，此处借助hutool工具
        byte[] bytes = FileUtil.readBytes(classPath);

        //加载
        return defineClass(className, bytes, 0, bytes.length);
    }
}
